package com.android.tvservicer

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.wifi.WifiManager
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.ArrayAdapter
import java.io.IOException
import java.net.InetAddress
import java.net.UnknownHostException
import java.util.*
import javax.jmdns.JmDNS
import javax.jmdns.ServiceInfo
import javax.jmdns.ServiceListener

class MainActivity : AppCompatActivity() {

    lateinit var serviceInfo: ServiceInfo
    var jmDNS: JmDNS? = null
    var listener: ServiceListener? = null
    lateinit var wifi: WifiManager
    lateinit var lock: WifiManager.MulticastLock
    val TAG = MainActivity::class.java.simpleName
    private val type = "_http._tcp.local."//_airplay._tcp.local.
    //Samsung-Galaxy-S7-edge-lan.local.
    //_raop._tcp.local.
    var mList: MutableList<String>? = null
    lateinit var adapter: ArrayAdapter<String>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        startService(Intent(this, RegisterBoardDeviceServer::class.java))
       // finish()
    }
//
//    override fun onStart() {
//        super.onStart()
//        loadData()
//    }


    private fun loadData() {
        Thread(object : Runnable {
            @SuppressLint("WifiManagerLeak")
            override fun run() {
                wifi = getSystemService(Context.WIFI_SERVICE) as WifiManager
                lock = wifi.createMulticastLock(javaClass.simpleName)
                lock.setReferenceCounted(true)
                try {
                    val localIpAddress = getLocalIpAddress()
                    lock.acquire()
                    jmDNS = JmDNS.create(localIpAddress)
                    /**
                     * 注册设备的服务类型
                     */
                    serviceInfo = ServiceInfo.create(type,
                            "AndroidTest", 7433,
                            "从Android测试")
                    serviceInfo.setText(getDeviceDetailsMap())
                    jmDNS?.registerService(serviceInfo)
                } catch (e: IOException) {
                    e.printStackTrace()
                    return
                }
            }
        }).start()
    }

    private fun getDeviceDetailsMap(): Map<String, String> {
        val info = HashMap<String, String>()
        info.put("device_name", "my_device_name")
        return info
    }

    private fun getLocalIpAddress(): InetAddress {
        @SuppressLint("WifiManagerLeak") val wifiManager = getSystemService(Context.WIFI_SERVICE) as WifiManager
        val connectionInfo = wifiManager.connectionInfo
        val ipAddress = connectionInfo.ipAddress
        var address: InetAddress? = null
        try {
            address = InetAddress.getByName(String.format(Locale.ENGLISH, "%d.%d.%d.%d", ipAddress and 0xff,
                    ipAddress shr 8 and 0xff, ipAddress shr 16 and 0xff, ipAddress shr 24 and 0xff))
        } catch (e: UnknownHostException) {
            e.printStackTrace()
        }
        return address!!
    }


}
